package org.example.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.api.UserApi;
import org.example.common.Result;
import org.example.mapper.UserMapper;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = UserApi.class, version = "1.0.0", timeout = 35000)
public class UserService implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<User> saveUser(User user) {
        if (userMapper.insert(user) == 1) {
            return Result.ok(user);
        }
        return Result.error("插入失败");
    }

    @Override
    public Result<User> getUserById(Long userId) {
        User user = userMapper.selectById(userId);
        return Result.ok(user);
    }

    @Override
    public Result<User> login(String username, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return Result.error("用户不存在");
        }
        if (!user.getPassword().equals(password)) {
            return Result.error("密码错误");
        }
        user.setPassword(null);
        return Result.ok(user);
    }
}
